package cn.imvc.app.models;

import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsReviews;
import cn.imvc.app.entities.User;
import cn.imvc.app.repositories.GoodsReviewsRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
import cn.imvc.data.util.JpqlUtil;

@Service
public class GoodsReviewsModelImpl implements GoodsReviewsModel {

	@Autowired
	GoodsReviewsRepository goodsReviewsRepository;
	@Autowired
	UserModel userModel;
	@Autowired
	GoodsModel goodsModel;
	@Autowired
	GoodsSkuModel goodsSkuModel;
	
	public LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> getRequestMap() {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<GoodsReviews> find(ViewRequest viewRequest) {
		if(viewRequest == null){
			viewRequest=new ViewRequest();
		}
		viewRequest.getSort().put("r.createdAt", "desc");
		return goodsReviewsRepository.findAll("r","GoodsReviews r",ModelUtil.OrderByIdDesc(viewRequest));
	}

	public GoodsReviews get(int id) {
		return goodsReviewsRepository.findOne(id);
	}

	public GoodsReviews save(GoodsReviews goodsReviews) {
		goodsReviews.setCreatedAt(new Date());
		goodsReviews.setUpdatedAt(new Date());
		return goodsReviewsRepository.saveAndFlush(goodsReviews);
	}

	public GoodsReviews update(GoodsReviews goodsReviews) {
		goodsReviews.setUpdatedAt(new Date());
		return goodsReviewsRepository.saveAndFlush(goodsReviews);
	}

	public void delete(int id) {
		goodsReviewsRepository.delete(id);
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}

	public void deleteByGoods(Goods goods) {
		goodsReviewsRepository.deleteByGoods(goods);
	}

	public Page<Object[]> findObjectPage(ViewRequest viewRequest) {
		if(viewRequest == null){
			viewRequest=new ViewRequest();
		}
		String sql = "select g.name,v.state,v.remark,u.name as username,v.createdAt,v.id from goods_reviews v left join user u on v.userId = u.id left join goods g on v.goodsId = g.id "
		+JpqlUtil.toWhere(viewRequest.getParams())+" order by v.updatedAt desc";
		PageRequest pageRequest = new PageRequest(viewRequest.getPage(), viewRequest.getSize());
		return goodsReviewsRepository.findAllSql(sql, pageRequest);
	}

	public void saveReviews(Goods goods) {
		 List<GoodsReviews> goodsReviewList = goodsReviewsRepository.findByGoodsAndState(goods,(byte)0);
		 if(goodsReviewList.isEmpty()){
			GoodsReviews goodsReviews = new GoodsReviews();
			goodsReviews.setGoods(goods);
			goodsReviews.setCreatedAt(new Date());
			goodsReviews.setUpdatedAt(new Date());
			goodsReviews.setState((byte)0);
			save(goodsReviews);
		 }else{
			GoodsReviews goodsReviews = goodsReviewList.get(0);
			update(goodsReviews);
		 }
	}

	public void updateReviews(GoodsReviews goodsReviews){
		GoodsReviews goodsReviewsTmp = get(goodsReviews.getId());
		User user = userModel.getSessionUser();
		goodsReviews.setUserId(user.getId());
		goodsReviews.setCreatedAt(goodsReviewsTmp.getCreatedAt());
		update(goodsReviews);
		Goods goods = goodsModel.get(goodsReviews.getGoods().getId());
		goods.setVerify(goodsReviews.getState() == -1 ? 0:goodsReviews.getState());
		goods.setVerifyRemark(goodsReviews.getRemark());
		if(goodsReviews.getState() == (byte)1){
			goods.setState((byte)(goods.getState()-10));
			goods.setSellTime(new Date());
		}else{
			goods.setState((byte)0);
		}
		Goods goodsTmp = goodsModel.updateVerify(goods);
		//同步sku状态
		goodsSkuModel.updateSkuState(goodsTmp);
	}
}
